# 作者: 肖老师
# 2024年11月12日04时45分19秒
# xxxrlmk@163.com


a = [x for x in range(10)]
print(a)

b = []
for x in range(10):
    b.append(x)
print(b)

# 2个for循环
a = [j for i in range(10) for j in range(i)]
print(a)

# 生成2维列表
a = [[col * row for col in range(5)] for row in range(5)]
# f = [[j for j in range(i)] for i in range(1, 12)]
# f = [[j for i in range(1, 12)] for j in range(i)] 会报错，i未被定义，运算优先级原因
# print(f)
print(a)
# 二维展为一维
c = [j for i in a for j in i]
# c = [j for j in i for i in a] 会报错，i未被定义，要先行为i指定遍历对象
print(c)

# 使用if
d = [x for x in range(10) if x % 2 == 0]  # 规则就是if放后面，记住就行
print(d)
# 使用if-else
e = [x if x % 2 == 0 else x ** 2 for x in range(10)]  # 规则就是if-else放前面，记住就行
print(e)
